<?php

class Application_Model_ElementMapper {

    protected $_dbTable;

    public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     *
     * @return Application_Model_DbTable_Element
     */
    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Element');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Element $element) {
        $data = array(
            'id' => $element->id(),
            'name' => $element->name(),
            'tech_id' => $element->tech_id(),
            'total_size' => $element->total_size(),
            'available_size' => $element->available_size(),
            'owner_id' => $element->owner_id(),
            'status' => $element->status(),
            'tracker' => $element->tracker(),
            'type' => $element->type(),
        );
        if (null === ($id = $element->id())) {
            unset($data['id']);
            return $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));
            return $id;
        }
    }

    public function find($id) {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return null;
        }
        $row = $result->current();
        $user_mapper = new Application_Model_UserMapper();
        $user = $user_mapper->find($row->owner_id);
        $return = new Application_Model_Element($row->id, $row->name, $row->tech_id, $row->total_size, $row->available_size, $row->owner_id, $row->status, $row->tracker, $row->type);
        $return->set_owner($user);
        return $return;
    }

    public function fetchAll($order = null) {
        $resultSet = $this->getDbTable()->fetchAll(null, $order);
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Element($row->id, $row->name, $row->tech_id, $row->total_size, $row->available_size, $row->owner_id, $row->status, $row->tracker, $row->type);
            $entries[] = $entry;
        }
        return $entries;
    }

    public function addToGroup($id, $gid) {
        $tbl = new Application_Model_DbTable_GroupElement();
        return $tbl->insert(array("element_id" => $id, "group_id" => $gid));
    }

    public function unlinkGroup($id, $gid) {
        $tbl = new Application_Model_DbTable_GroupElement();
        return $tbl->delete("element_id=$id AND group_id=$gid");
    }

        public function findLinkedComments($id) {
        $rowset = $this->getDbTable()->find($id);
        if (0 == count($rowset)) {
            return null;
        }
        $row = $rowset->current();
        $comment_rowset = $row->findManyToManyRowset("Application_Model_DbTable_Comment", "Application_Model_DbTable_CommentLink");
        if ($comment_rowset) {
            $umapper = new Application_Model_UserMapper();
            $result = array();
            foreach ($comment_rowset as $row) {
                $entry = new Application_Model_Comment($row->id, $row->user_id, $row->time, $row->content);
                $entry->set_user($umapper->find($entry->user_id()));
                $result[] = $entry;
            }
            usort($result, create_function('$comment1, $comment2', 'return $comment1->time() - $comment2->time();'));
            return $result;
        } else {
            return null;
        }
    }
    
    public function remove($id) {
        return $this->getDbTable()->delete("id=$id");
    }
}
